import os
from time import sleep
from support.sys.shell import Shell as shell
from support.utils.toolkit import ToolKit
from support.utils.constants import Constants
from support.network.push import MediaPush

class BiliStreaming:
    DATABASE_NAME = "Bilibili"
    TAG = "BiliStreaming"
    TOOLPATH = os.path.join(os.path.dirname(os.path.dirname(__file__)),"tools","bilibilistreaming") 
    
    def updateCookie(self, cookie):
        with open(os.path.join(BiliStreaming.TOOLPATH, "cookie.txt"), "w") as file:
            file.write(cookie)

    def start(self, cookie):
        self.updateCookie(cookie)
        return shell.executeP("./BilibiliLiver", workdir=BiliStreaming.TOOLPATH)

    def untilRemoteServerOpend(self):
        openDate = ToolKit.getSpecialTime(hour=Constants.DEFAULT_OPEN_TIME)
        closeDate = ToolKit.getSpecialTime(hour=Constants.DEFAULT_CLOSE_TIME, limitToday=True)
        if((openDate < closeDate) or (ToolKit.getTimeNow() > closeDate)):
            interval = ToolKit.calcIntervalSeconds(openDate)
            if(interval > 5 * 60):
                sleep(interval - 5 * 60)
        return True
